import 'package:baselib/utils/log_util.dart';
import 'package:baselib/utils/string_base_util.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:permission_handler/permission_handler.dart';

import 'toast_util.dart';

/**
 * 统一处理权限
 */
class CheckPermissionUtil{

  static Future<bool> checkCommPermission(Permission permission,{bool isShowToast:true}) async{
    PermissionStatus permissionStatus = await permission.status;
    LogUtil.d('checkCommPermission-->permission=$permission,permissionStatus=$permissionStatus');
    if(permissionStatus == PermissionStatus.granted){
      return true;
    }else{
      PermissionStatus permissionStatusTemp = await permission.request();
      LogUtil.d('checkCommPermission request-->permission=$permission,permissionStatus=$permissionStatus');
      if(permissionStatusTemp == PermissionStatus.granted){
        return true;
      }else{
        // if((isShowToast??false))ToastUtil.toast(StringBaseUtil().qdszlmdkyysqqx);
        showCupertinoDialog(
            context: Get.context,
            builder: (context) {
              return CupertinoAlertDialog(
                title: Text('You need to grant ${permission} permissions'),
                content: Text(
                    'Please go to your mobile phone to set the permission to open the corresponding ${permission}'),
                actions: <Widget>[
                  CupertinoDialogAction(
                    child: const Text('cancle'),
                    onPressed: () {
                      Navigator.pop(context);
                    },
                  ),
                  CupertinoDialogAction(
                    child: const Text('confirm'),
                    onPressed: () {
                      Navigator.pop(context);
                      // 打开手机上该app权限的页面
                      openAppSettings();
                    },
                  ),
                ],
              );
            });
        return false;
      }
    }
  }
}